<?php

namespace app\shop\controller\operate;

use app\common\model\store\Store;
use app\shop\controller\Controller;
use app\shop\model\ad\Ad as AdModel;
use app\shop\model\ad\AdCategory as AdCategoryModel;
use app\shop\model\supplier\Supplier as SupplierModel;

/**
 * 广告控制器
 */
class market extends Controller
{
    /**
     * 广告数据
     */
    public function index()
    {
        $model = new AdModel;

        $list = $model->getList($this->postData());

        $shop_supplier_array = (new SupplierModel())->where('is_delete',0) -> column('name','shop_supplier_id');

        $store_list = Store::where('is_delete',0) -> field('store_name,shop_supplier_id') -> select();
        $store_array = [];
        foreach ($store_list as $v){
            $store_array[$v['shop_supplier_id']][] = $v['store_name'];
//            if(isset($store_array[$v['shop_supplier_id']])){
//                $store_array[$v['shop_supplier_id']] = $store_array[$v['shop_supplier_id']].','.$v['store_name'];
//            }else{
//                $store_array[$v['shop_supplier_id']] = $v['store_name'];
//            }
        }


        $ad_type = config('ad');
        foreach ($ad_type as $k => $v){
            $array[$v['key']] = $v['value'];
        }
        foreach ($list as $key => $value){
            $list[$key]['type_text'] = $array[$value['type']]??'';
            $list[$key]['shop_supplier_name'] = $shop_supplier_array[$value['shop_supplier_id']]??'';
            $list[$key]['store_name'] = $store_array[$value['shop_supplier_id']]??'';
        }
        return $this->renderSuccess('',compact('list'));
    }

    /**
     * 添加广告
     */
    public function add()
    {   
        if($this->request->isGet()){
            return $this->renderSuccess('');
        }
        // 新增记录
        $model = new AdModel;
        $data = $this->postData();
        $select_list = $data['select_list']??[];

        if($data['region'] == 2 && !$select_list){
            return $this->renderError('请选择商户');
        }
        if ($model->add($data)) {
             return $this->renderSuccess('添加成功');
        }

        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 广告详情
     */
    public function detail($ad_id)
    {
        // 广告详情
        $detail = AdModel::detail($ad_id);
        $catgory = AdCategoryModel::getAll();
        return $this->renderSuccess('',compact('detail','catgory'));

    }

    /**
     * 修改
     * @param $id
     * @return \think\response\Json
     */
    public function edit($ad_id)
    {
        if($this->request->isGet()){
            return $this->detail($ad_id);
        }
        $model = AdModel::detail($ad_id);
        // 更新记录
        if ($model->edit($this->postData())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除记录
     */
    public function delete($ad_id)
    {
        $model = AdModel::detail($ad_id);
        if ($model->remove($ad_id)) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?:'删除失败');
    }

    /**
     *获取广告类型
     * getAdTypeList
     * void
     * @return void
     * @Author kevin
     * @date 2024/4/3 9:47
     */
    public function getAdTypeList(){
        return $this->renderSuccess('', config('ad'));
    }



}
